# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\PyInstaller\lib\modulegraph\modulegraph.py
"""
Find modules used by a script, using bytecode analysis.

Based on the stdlib modulefinder by Thomas Heller and Just van Rossum,
but uses a graph data structure and 2.3 features

XXX: Verify all calls to _import_hook (and variants) to ensure that
imports are done in the right way.
"""
from __future__ import absolute_import, print_function
import pkg_resources, ast, dis, imp, marshal, os, pkgutil, sys, re
from collections import deque, namedtuple
from struct import unpack
from ..altgraph.ObjectGraph import ObjectGraph
from ..altgraph import GraphError
from . import util
from . import zipio
from ._compat import (
    get_instructions,
    BytesIO,
    StringIO,
    pathname2url,
    _cOrd,
    _READ_MODE,
)
import codecs

BOM = codecs.BOM_UTF8.decode("utf-8")
ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL = -1
ABSOLUTE_IMPORT_LEVEL = 0
DEFAULT_IMPORT_LEVEL = (
    ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL
    if sys.version_info[0] == 2
    else ABSOLUTE_IMPORT_LEVEL
)
_IMPORTABLE_FILETYPE_TO_METADATA = {
    filetype: (filetype, open_mode, imp_type)
    for filetype, open_mode, imp_type in imp.get_suffixes()
}
_packagePathMap = {}
_SETUPTOOLS_NAMESPACEPKG_PTHs = (
    "import sys, types, os;has_mfs = sys.version_info > (3, 5);p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('",
    "import sys,types,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('",
    "import sys,new,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('",
    "import sys, types, os;p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('",
    "import sys, types, os;pep420 = sys.version_info > (3, 3);p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('",
)


def _namespace_package_path(fqname, pathnames, path=None):
    """
    Return the __path__ for the python package in *fqname*.
    
    This function uses setuptools metadata to extract information
    about namespace packages from installed eggs.
    """
    working_set = pkg_resources.WorkingSet(path)
    path = list(pathnames)
    for dist in working_set:
        if dist.has_metadata("namespace_packages.txt"):
            namespaces = dist.get_metadata("namespace_packages.txt").splitlines()
            if fqname in namespaces:
                nspath = os.path.join(dist.location, *fqname.split("."))
                if nspath not in path:
                    path.append(nspath)

    return path


_strs = re.compile("^\\s*[\"']([A-Za-z0-9_]+)[\"'],?\\s*")


def _eval_str_tuple(value):
    """
    Input is the repr of a tuple of strings, output
    is that tuple.
    
    This only works with a tuple where the members are
    python identifiers.
    """
    if not (value.startswith("(") and value.endswith(")")):
        raise ValueError(value)
    orig_value = value
    value = value[1:-1]
    result = []
    while value:
        m = _strs.match(value)
        if m is None:
            raise ValueError(orig_value)
        result.append(m.group(1))
        value = value[len(m.group(0)) :]

    return tuple(result)


def _path_from_importerror(exc, default):
    m = re.match("^No module named (\\S+)$", str(exc))
    if m is not None:
        return m.group(1)
    else:
        return default


def os_listdir(path):
    """
    Deprecated name
    """
    warnings.warn("Use zipio.listdir instead of os_listdir", DeprecationWarning)
    return zipio.listdir(path)


def _code_to_file(co):
    """ Convert code object to a .pyc pseudo-file """
    return BytesIO(imp.get_magic() + b"\x00\x00\x00\x00" + marshal.dumps(co))


def moduleInfoForPath(path):
    for ext, readmode, typ in imp.get_suffixes():
        if path.endswith(ext):
            return (os.path.basename(path)[: -len(ext)], readmode, typ)


import warnings


def AddPackagePath(packagename, path):
    warnings.warn("Use addPackagePath instead of AddPackagePath", DeprecationWarning)
    addPackagePath(packagename, path)


def addPackagePath(packagename, path):
    paths = _packagePathMap.get(packagename, [])
    paths.append(path)
    _packagePathMap[packagename] = paths


_replacePackageMap = {}


def ReplacePackage(oldname, newname):
    warnings.warn("use replacePackage instead of ReplacePackage", DeprecationWarning)
    replacePackage(oldname, newname)


def replacePackage(oldname, newname):
    _replacePackageMap[oldname] = newname


class DependencyInfo(
    namedtuple("DependencyInfo", ["conditional", "function", "tryexcept", "fromlist"])
):
    __slots__ = ()

    def _merged(self, other):
        if (
            not self.conditional
            and not self.function
            and not self.tryexcept
            or not other.conditional
            and not other.function
            and not other.tryexcept
        ):
            return DependencyInfo(
                conditional=False,
                function=False,
                tryexcept=False,
                fromlist=self.fromlist and other.fromlist,
            )
        else:
            return DependencyInfo(
                conditional=self.conditional or other.conditional,
                function=self.function or other.function,
                tryexcept=self.tryexcept or other.tryexcept,
                fromlist=self.fromlist and other.fromlist,
            )


class Node(object):
    """
    Abstract base class (ABC) of all objects added to a `ModuleGraph`.
    
    Attributes
    ----------
    code : codeobject
        Code object of the pure-Python module corresponding to this graph node
        if any _or_ `None` otherwise.
    graphident : str
        Synonym of `identifier` required by the `ObjectGraph` superclass of the
        `ModuleGraph` class. For readability, the `identifier` attribute should
        typically be used instead.
    filename : str
        Absolute path of this graph node's corresponding module, package, or C
        extension if any _or_ `None` otherwise.
    identifier : str
        Fully-qualified name of this graph node's corresponding module,
        package, or C extension.
    packagepath : str
        List of the absolute paths of all directories comprising this graph
        node's corresponding package. If this is a:
        * Non-namespace package, this list contains exactly one path.
        * Namespace package, this list contains one or more paths.
    _deferred_imports : list
        List of all target modules imported by the source module corresponding
        to this graph node whole importations have been deferred for subsequent
        processing in between calls to the `_ModuleGraph._scan_code()` and
        `_ModuleGraph._process_imports()` methods for this source module _or_
        `None` otherwise. Each element of this list is a 3-tuple
        `(have_star, _safe_import_hook_args, _safe_import_hook_kwargs)`
        collecting the importation of a target module from this source module
        for subsequent processing, where:
        * `have_star` is a boolean `True` only if this is a `from`-style star
          import (e.g., resembling `from {target_module_name} import *`).
        * `_safe_import_hook_args` is a (typically non-empty) sequence of all
          positional arguments to be passed to the `_safe_import_hook()` method
          to add this importation to the graph.
        * `_safe_import_hook_kwargs` is a (typically empty) dictionary of all
          keyword arguments to be passed to the `_safe_import_hook()` method
          to add this importation to the graph.
        Unlike functional languages, Python imposes a maximum depth on the
        interpreter stack (and hence recursion). On breaching this depth,
        Python raises a fatal `RuntimeError` exception. Since `ModuleGraph`
        parses imports recursively rather than iteratively, this depth _was_
        commonly breached before the introduction of this list. Python
        environments installing a large number of modules (e.g., Anaconda) were
        particularly susceptible. Why? Because `ModuleGraph` concurrently
        descended through both the abstract syntax trees (ASTs) of all source
        modules being parsed _and_ the graph of all target modules imported by
        these source modules being built. The stack thus consisted of
        alternating layers of AST and graph traversal. To unwind such
        alternation and effectively halve the stack depth, `ModuleGraph` now
        descends through the abstract syntax tree (AST) of each source module
        being parsed and adds all importations originating within this module
        to this list _before_ descending into the graph of these importations.
        See pyinstaller/pyinstaller/#1289 for further details.
    _global_attr_names : set
        Set of the unqualified names of all global attributes (e.g., classes,
        variables) defined in the pure-Python module corresponding to this
        graph node if any _or_ the empty set otherwise. This includes the names
        of all attributes imported via `from`-style star imports from other
        existing modules (e.g., `from {target_module_name} import *`). This
        set is principally used to differentiate the non-ignorable importation
        of non-existent submodules in a package from the ignorable importation
        of existing global attributes defined in that package's pure-Python
        `__init__` submodule in `from`-style imports (e.g., `bar` in
        `from foo import bar`, which may be either a submodule or attribute of
        `foo`), as such imports ambiguously allow both. This set is _not_ used
        to differentiate submodules from attributes in `import`-style imports
        (e.g., `bar` in `import foo.bar`, which _must_ be a submodule of
        `foo`), as such imports unambiguously allow only submodules.
    _starimported_ignored_module_names : set
        Set of the fully-qualified names of all existing unparsable modules
        that the existing parsable module corresponding to this graph node
        attempted to perform one or more "star imports" from. If this module
        either does _not_ exist or does but is unparsable, this is the empty
        set. Equivalently, this set contains each fully-qualified name
        `{trg_module_name}` for which:
        * This module contains an import statement of the form
          `from {trg_module_name} import *`.
        * The module whose name is `{trg_module_name}` exists but is _not_
          parsable by `ModuleGraph` (e.g., due to _not_ being pure-Python).
        **This set is currently defined but otherwise ignored.**
    _submodule_basename_to_node : dict
        Dictionary mapping from the unqualified name of each submodule
        contained by the parent module corresponding to this graph node to that
        submodule's graph node. If this dictionary is non-empty, this parent
        module is typically but _not_ always a package (e.g., the non-package
        `os` module containing the `os.path` submodule).
    """

    __slots__ = [
        "code",
        "filename",
        "graphident",
        "identifier",
        "packagepath",
        "_deferred_imports",
        "_global_attr_names",
        "_starimported_ignored_module_names",
        "_submodule_basename_to_node",
    ]

    def __init__(self, identifier):
        """
        Initialize this graph node.
        
        Parameters
        ----------
        identifier : str
            Fully-qualified name of this graph node's corresponding module,
            package, or C extension.
        """
        self.code = None
        self.filename = None
        self.graphident = identifier
        self.identifier = identifier
        self.packagepath = None
        self._deferred_imports = None
        self._global_attr_names = set()
        self._starimported_ignored_module_names = set()
        self._submodule_basename_to_node = dict()

    def is_global_attr(self, attr_name):
        """
        `True` only if the pure-Python module corresponding to this graph node
        defines a global attribute (e.g., class, variable) with the passed
        name.
        
        If this module is actually a package, this method instead returns
        `True` only if this package's pure-Python `__init__` submodule defines
        such a global attribute. In this case, note that this package may still
        contain an importable submodule of the same name. Callers should
        attempt to import this attribute as a submodule of this package
        _before_ assuming this attribute to be an ignorable global. See
        "Examples" below for further details.
        
        Parameters
        ----------
        attr_name : str
            Unqualified name of the attribute to be tested.
        
        Returns
        ----------
        bool
            `True` only if this module defines this global attribute.
        
        Examples
        ----------
        Consider a hypothetical module `foo` containing submodules `bar` and
        `__init__` where the latter assigns `bar` to be a global variable
        (possibly star-exported via the special `__all__` global variable):
        
        >>> # In "foo.__init__":
        >>> bar = 3.1415
        
        Python 2 and 3 both permissively permit this. This method returns
        `True` in this case (i.e., when called on the `foo` package's graph
        node, passed the attribute name `bar`) despite the importability of the
        `foo.bar` submodule.
        """
        return attr_name in self._global_attr_names

    def is_submodule(self, submodule_basename):
        """
        `True` only if the parent module corresponding to this graph node
        contains the submodule with the passed name.
        
        If `True`, this parent module is typically but _not_ always a package
        (e.g., the non-package `os` module containing the `os.path` submodule).
        
        Parameters
        ----------
        submodule_basename : str
            Unqualified name of the submodule to be tested.
        
        Returns
        ----------
        bool
            `True` only if this parent module contains this submodule.
        """
        return submodule_basename in self._submodule_basename_to_node

    def add_global_attr(self, attr_name):
        """
        Record the global attribute (e.g., class, variable) with the passed
        name to be defined by the pure-Python module corresponding to this
        graph node.
        
        If this module is actually a package, this method instead records this
        attribute to be defined by this package's pure-Python `__init__`
        submodule.
        
        Parameters
        ----------
        attr_name : str
            Unqualified name of the attribute to be added.
        """
        self._global_attr_names.add(attr_name)

    def add_global_attrs_from_module(self, target_module):
        """
        Record all global attributes (e.g., classes, variables) defined by the
        target module corresponding to the passed graph node to also be defined
        by the source module corresponding to this graph node.
        
        If the source module is actually a package, this method instead records
        these attributes to be defined by this package's pure-Python `__init__`
        submodule.
        
        Parameters
        ----------
        target_module : Node
            Graph node of the target module to import attributes from.
        """
        self._global_attr_names.update(target_module._global_attr_names)

    def add_submodule(self, submodule_basename, submodule_node):
        """
        Add the submodule with the passed name and previously imported graph
        node to the parent module corresponding to this graph node.
        
        This parent module is typically but _not_ always a package (e.g., the
        non-package `os` module containing the `os.path` submodule).
        
        Parameters
        ----------
        submodule_basename : str
            Unqualified name of the submodule to add to this parent module.
        submodule_node : Node
            Graph node of this submodule.
        """
        self._submodule_basename_to_node[submodule_basename] = submodule_node

    def get_submodule(self, submodule_basename):
        """
        Graph node of the submodule with the passed name in the parent module
        corresponding to this graph node.
        
        If this parent module does _not_ contain this submodule, an exception
        is raised. Else, this parent module is typically but _not_ always a
        package (e.g., the non-package `os` module containing the `os.path`
        submodule).
        
        Parameters
        ----------
        module_basename : str
            Unqualified name of the submodule to retrieve.
        
        Returns
        ----------
        Node
            Graph node of this submodule.
        """
        return self._submodule_basename_to_node[submodule_basename]

    def get_submodule_or_none(self, submodule_basename):
        """
        Graph node of the submodule with the passed unqualified name in the
        parent module corresponding to this graph node if this module contains
        this submodule _or_ `None`.
        
        This parent module is typically but _not_ always a package (e.g., the
        non-package `os` module containing the `os.path` submodule).
        
        Parameters
        ----------
        submodule_basename : str
            Unqualified name of the submodule to retrieve.
        
        Returns
        ----------
        Node
            Graph node of this submodule if this parent module contains this
            submodule _or_ `None`.
        """
        return self._submodule_basename_to_node.get(submodule_basename)

    def remove_global_attr_if_found(self, attr_name):
        """
        Record the global attribute (e.g., class, variable) with the passed
        name if previously recorded as defined by the pure-Python module
        corresponding to this graph node to be subsequently undefined by the
        same module.
        
        If this module is actually a package, this method instead records this
        attribute to be undefined by this package's pure-Python `__init__`
        submodule.
        
        This method is intended to be called on globals previously defined by
        this module that are subsequently undefined via the `del` built-in by
        this module, thus "forgetting" or "undoing" these globals.
        
        For safety, there exists no corresponding `remove_global_attr()`
        method. While defining this method is trivial, doing so would invite
        `KeyError` exceptions on scanning valid Python that lexically deletes a
        global in a scope under this module's top level (e.g., in a function)
        _before_ defining this global at this top level. Since `ModuleGraph`
        cannot and should not (re)implement a full-blown Python interpreter,
        ignoring out-of-order deletions is the only sane policy.
        
        Parameters
        ----------
        attr_name : str
            Unqualified name of the attribute to be removed.
        """
        if self.is_global_attr(attr_name):
            self._global_attr_names.remove(attr_name)

    def __cmp__(self, other):
        try:
            otherIdent = getattr(other, "graphident")
        except AttributeError:
            return NotImplemented

        return cmp(self.graphident, otherIdent)

    def __eq__(self, other):
        try:
            otherIdent = getattr(other, "graphident")
        except AttributeError:
            return False

        return self.graphident == otherIdent

    def __ne__(self, other):
        try:
            otherIdent = getattr(other, "graphident")
        except AttributeError:
            return True

        return self.graphident != otherIdent

    def __lt__(self, other):
        try:
            otherIdent = getattr(other, "graphident")
        except AttributeError:
            return NotImplemented

        return self.graphident < otherIdent

    def __le__(self, other):
        try:
            otherIdent = getattr(other, "graphident")
        except AttributeError:
            return NotImplemented

        return self.graphident <= otherIdent

    def __gt__(self, other):
        try:
            otherIdent = getattr(other, "graphident")
        except AttributeError:
            return NotImplemented

        return self.graphident > otherIdent

    def __ge__(self, other):
        try:
            otherIdent = getattr(other, "graphident")
        except AttributeError:
            return NotImplemented

        return self.graphident >= otherIdent

    def __hash__(self):
        return hash(self.graphident)

    def infoTuple(self):
        return (self.identifier,)

    def __repr__(self):
        return "%s%r" % (type(self).__name__, self.infoTuple())


class Alias(str):
    """
    Placeholder aliasing an existing source module to a non-existent target
    module (i.e., the desired alias).
    
    For obscure reasons, this class subclasses `str`. Each instance of this
    class is the fully-qualified name of the existing source module being
    aliased. Unlike the related `AliasNode` class, instances of this class are
    _not_ actual nodes and hence _not_ added to the graph; they only facilitate
    communication between the `ModuleGraph.alias_module()` and
    `ModuleGraph.findNode()` methods.
    """

    pass


class AliasNode(Node):
    """
    Graph node representing the aliasing of an existing source module under a
    non-existent target module name (i.e., the desired alias).
    """

    def __init__(self, name, node):
        """
        Initialize this alias.
        
        Parameters
        ----------
        name : str
            Fully-qualified name of the non-existent target module to be
            created (as an alias of the existing source module).
        node : Node
            Graph node of the existing source module being aliased.
        """
        super(AliasNode, self).__init__(name)
        for attr_name in (
            "identifier",
            "packagepath",
            "_global_attr_names",
            "_starimported_ignored_module_names",
            "_submodule_basename_to_node",
        ):
            if hasattr(node, attr_name):
                setattr(self, attr_name, getattr(node, attr_name))

    def infoTuple(self):
        return (self.graphident, self.identifier)


class BadModule(Node):
    pass


class ExcludedModule(BadModule):
    pass


class MissingModule(BadModule):
    pass


class Script(Node):
    def __init__(self, filename):
        super(Script, self).__init__(filename)
        self.filename = filename

    def infoTuple(self):
        return (self.filename,)


class BaseModule(Node):
    def __init__(self, name, filename=None, path=None):
        super(BaseModule, self).__init__(name)
        self.filename = filename
        self.packagepath = path

    def infoTuple(self):
        return tuple(filter(None, (self.identifier, self.filename, self.packagepath)))


class BuiltinModule(BaseModule):
    pass


class SourceModule(BaseModule):
    pass


class InvalidSourceModule(SourceModule):
    pass


class CompiledModule(BaseModule):
    pass


class InvalidCompiledModule(BaseModule):
    pass


class Extension(BaseModule):
    pass


class Package(BaseModule):
    """
    Graph node representing a non-namespace package.
    """

    pass


class NamespacePackage(Package):
    """
    Graph node representing a namespace package.
    """

    pass


class RuntimeModule(BaseModule):
    """
    Graph node representing a non-package Python module dynamically defined at
    runtime.
    
    Most modules are statically defined on-disk as standard Python files.
    Some modules, however, are dynamically defined in-memory at runtime
    (e.g., `gi.repository.Gst`, dynamically defined by the statically
    defined `gi.repository.__init__` module).
    
    This node represents such a runtime module. Since this is _not_ a package,
    all attempts to import submodules from this module in `from`-style import
    statements (e.g., the `queue` submodule in `from six.moves import queue`)
    will be silently ignored.
    
    To ensure that the parent package of this module if any is also imported
    and added to the graph, this node is typically added to the graph by
    calling the `ModuleGraph.add_module()` method.
    """

    pass


class RuntimePackage(Package):
    """
    Graph node representing a non-namespace Python package dynamically defined
    at runtime.
    
    Most packages are statically defined on-disk as standard subdirectories
    containing `__init__.py` files. Some packages, however, are dynamically
    defined in-memory at runtime (e.g., `six.moves`, dynamically defined by
    the statically defined `six` module).
    
    This node represents such a runtime package. All attributes imported from
    this package in `from`-style import statements that are submodules of this
    package (e.g., the `queue` submodule in `from six.moves import queue`) will
    be imported rather than ignored.
    
    To ensure that the parent package of this package if any is also imported
    and added to the graph, this node is typically added to the graph by
    calling the `ModuleGraph.add_module()` method.
    """

    pass


class FlatPackage(BaseModule):
    def __init__(self, *args, **kwds):
        warnings.warn(
            "This class will be removed in a future version of modulegraph",
            DeprecationWarning,
        )
        super(FlatPackage, *args, **kwds)


class ArchiveModule(BaseModule):
    def __init__(self, *args, **kwds):
        warnings.warn(
            "This class will be removed in a future version of modulegraph",
            DeprecationWarning,
        )
        super(FlatPackage, *args, **kwds)


header = "<html>\n  <head>\n    <title>%(TITLE)s</title>\n    <style>\n      .node { padding: 0.5em 0 0.5em; border-top: thin grey dotted; }\n      .moduletype { font: smaller italic }\n      .node a { text-decoration: none; color: #006699; }\n      .node a:visited { text-decoration: none; color: #2f0099; }\n    </style>\n  </head>\n  <body>\n    <h1>%(TITLE)s</h1>"
entry = '\n<div class="node">\n  <a name="%(NAME)s"></a>\n  %(CONTENT)s\n</div>'
contpl = '<tt>%(NAME)s</tt> <span class="moduletype">%(TYPE)s</span>'
contpl_linked = '<a target="code" href="%(URL)s" type="text/plain"><tt>%(NAME)s</tt></a>\n<span class="moduletype">%(TYPE)s</span>'
imports = '  <div class="import">\n%(HEAD)s:\n  %(LINKS)s\n  </div>\n'
footer = "\n  </body>\n</html>"


def _ast_names(names):
    result = []
    for nm in names:
        if isinstance(nm, ast.alias):
            result.append(nm.name)
        else:
            result.append(nm)

    result = [r for r in result if r != "__main__"]
    return result


def uniq(seq):
    """Remove duplicates from a list, preserving order"""
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x in seen or seen_add(x))]


if sys.version_info[0] == 2:
    DEFAULT_IMPORT_LEVEL = -1
else:
    DEFAULT_IMPORT_LEVEL = 0


class _Visitor(ast.NodeVisitor):
    def __init__(self, graph, module):
        self._graph = graph
        self._module = module
        self._level = DEFAULT_IMPORT_LEVEL
        self._in_if = [False]
        self._in_def = [False]
        self._in_tryexcept = [False]

    @property
    def in_if(self):
        return self._in_if[-1]

    @property
    def in_def(self):
        return self._in_def[-1]

    @property
    def in_tryexcept(self):
        return self._in_tryexcept[-1]

    def _collect_import(self, name, fromlist, level):
        if sys.version_info[0] == 2:
            if name == "__future__":
                if "absolute_import" in (fromlist or ()):
                    self._level = 0
        have_star = False
        if fromlist is not None:
            fromlist = uniq(fromlist)
            if "*" in fromlist:
                fromlist.remove("*")
                have_star = True
        self._module._deferred_imports.append(
            (
                have_star,
                (name, self._module, fromlist, level),
                {
                    "edge_attr": DependencyInfo(
                        conditional=self.in_if,
                        tryexcept=self.in_tryexcept,
                        function=self.in_def,
                        fromlist=False,
                    )
                },
            )
        )

    def visit_Import(self, node):
        for nm in _ast_names(node.names):
            self._collect_import(nm, None, self._level)

    def visit_ImportFrom(self, node):
        level = node.level if node.level != 0 else self._level
        self._collect_import(node.module or "", _ast_names(node.names), level)

    def visit_If(self, node):
        self._in_if.append(True)
        self.generic_visit(node)
        self._in_if.pop()

    def visit_FunctionDef(self, node):
        self._in_def.append(True)
        self.generic_visit(node)
        self._in_def.pop()

    def visit_Try(self, node):
        self._in_tryexcept.append(True)
        self.generic_visit(node)
        self._in_tryexcept.pop()

    def visit_ExceptHandler(self, node):
        self._in_tryexcept.append(True)
        self.generic_visit(node)
        self._in_tryexcept.pop()

    def visit_TryExcept(self, node):
        self._in_tryexcept.append(True)
        self.generic_visit(node)
        self._in_tryexcept.pop()

    def visit_Expression(self, node):
        pass

    visit_BoolOp = visit_Expression
    visit_BinOp = visit_Expression
    visit_UnaryOp = visit_Expression
    visit_Lambda = visit_Expression
    visit_IfExp = visit_Expression
    visit_Dict = visit_Expression
    visit_Set = visit_Expression
    visit_ListComp = visit_Expression
    visit_SetComp = visit_Expression
    visit_ListComp = visit_Expression
    visit_GeneratorExp = visit_Expression
    visit_Compare = visit_Expression
    visit_Yield = visit_Expression
    visit_YieldFrom = visit_Expression
    visit_Await = visit_Expression
    visit_Call = visit_Expression


class ModuleGraph(ObjectGraph):
    """
    Directed graph whose nodes represent modules and edges represent
    dependencies between these modules.
    """

    def createNode(self, cls, name, *args, **kw):
        m = self.findNode(name)
        if m is None:
            m = super(ModuleGraph, self).createNode(cls, name, *args, **kw)
        return m

    def __init__(
        self, path=None, excludes=(), replace_paths=(), implies=(), graph=None, debug=0
    ):
        super(ModuleGraph, self).__init__(graph=graph, debug=debug)
        if path is None:
            path = sys.path
        self.path = path
        self.lazynodes = {}
        self.lazynodes.update(dict(implies))
        for m in excludes:
            self.lazynodes[m] = None

        self.replace_paths = replace_paths
        self.set_setuptools_nspackages()
        self._package_path_map = _packagePathMap

    def set_setuptools_nspackages(self):
        self.nspackages = self._calc_setuptools_nspackages()

    def _calc_setuptools_nspackages(self):
        pkgmap = {}
        try:
            from pkgutil import ImpImporter
        except ImportError:
            try:
                from _pkgutil import ImpImporter
            except ImportError:
                ImpImporter = pkg_resources.ImpWrapper

        if sys.version_info[:2] >= (3, 3):
            import importlib.machinery

            ImpImporter = importlib.machinery.FileFinder
        for entry in self.path:
            importer = pkg_resources.get_importer(entry)
            if isinstance(importer, ImpImporter):
                try:
                    ldir = os.listdir(entry)
                except os.error:
                    continue

                for fn in ldir:
                    if fn.endswith("-nspkg.pth"):
                        fp = open(os.path.join(entry, fn), "rU")
                        try:
                            for ln in fp:
                                for pfx in _SETUPTOOLS_NAMESPACEPKG_PTHs:
                                    if ln.startswith(pfx):
                                        try:
                                            start = len(pfx) - 2
                                            stop = ln.index(")", start) + 1
                                        except ValueError:
                                            continue

                                        pkg = _eval_str_tuple(ln[start:stop])
                                        identifier = (".").join(pkg)
                                        subdir = os.path.join(entry, *pkg)
                                        if os.path.exists(
                                            os.path.join(subdir, "__init__.py")
                                        ):
                                            pass
                                        else:
                                            if identifier in pkgmap:
                                                pkgmap[identifier].append(subdir)
                                            else:
                                                pkgmap[identifier] = [subdir]
                                            break

                        finally:
                            fp.close()

        return pkgmap

    def implyNodeReference(self, node, other, edge_data=None):
        """
        Create a reference from the passed source node to the passed other node,
        implying the former to depend upon the latter.
        
        While the source node _must_ be an existing graph node, the target node
        may be either an existing graph node _or_ a fully-qualified module name.
        In the latter case, the module with that name and all parent packages of
        that module will be imported _without_ raising exceptions and for each
        newly imported module or package:
        
        * A new graph node will be created for that module or package.
        * A reference from the passed source node to that module or package will
          be created.
        
        This method allows dependencies between Python objects _not_ importable
        with standard techniques (e.g., module aliases, C extensions).
        
        Parameters
        ----------
        node : str
            Graph node for this reference's source module or package.
        other : {Node, str}
            Either a graph node _or_ fully-qualified name for this reference's
            target module or package.
        """
        if isinstance(other, Node):
            self._updateReference(node, other, edge_data)
        else:
            if isinstance(other, tuple):
                raise ValueError(other)
            others = self._safe_import_hook(other, node, None)
            for other in others:
                self._updateReference(node, other, edge_data)

    def getReferences(self, fromnode):
        """
        Yield all nodes that `fromnode` dependes on (that is,
        all modules that `fromnode` imports.
        """
        node = self.findNode(fromnode)
        out_edges, _ = self.get_edges(node)
        return out_edges

    def getReferers(self, tonode, collapse_missing_modules=True):
        node = self.findNode(tonode)
        _, in_edges = self.get_edges(node)
        if collapse_missing_modules:
            for n in in_edges:
                if isinstance(n, MissingModule):
                    for n in self.getReferers(n, False):
                        yield n

                else:
                    yield n

        else:
            for n in in_edges:
                yield n

    def hasEdge(self, fromnode, tonode):
        """ Return True iff there is an edge from 'fromnode' to 'tonode' """
        fromnode = self.findNode(fromnode)
        tonode = self.findNode(tonode)
        return self.graph.edge_by_node(fromnode, tonode) is not None

    def foldReferences(self, packagenode):
        """
        Create edges to/from `packagenode` based on the edges to/from all
        submodules of that package _and_ then hide the graph nodes
        corresponding to those submodules.
        """
        pkg = self.findNode(packagenode)
        for n in self.nodes():
            if not n.identifier.startswith(pkg.identifier + "."):
                continue
            iter_out, iter_inc = n.get_edges()
            for other in iter_out:
                if other.identifier.startswith(pkg.identifier + "."):
                    continue
                if not self.hasEdge(pkg, other):
                    self._updateReference(pkg, other, "pkg-internal-import")

            for other in iter_in:
                if other.identifier.startswith(pkg.identifier + "."):
                    continue
                if not self.hasEdge(other, pkg):
                    self._updateReference(other, pkg, "pkg-import")

            self.graph.hide_node(n)

    def _updateReference(self, fromnode, tonode, edge_data):
        try:
            ed = self.edgeData(fromnode, tonode)
        except (KeyError, GraphError):
            return self.createReference(fromnode, tonode, edge_data)
        else:
            if not (
                isinstance(ed, DependencyInfo) and isinstance(edge_data, DependencyInfo)
            ):
                self.updateEdgeData(fromnode, tonode, edge_data)
            else:
                self.updateEdgeData(fromnode, tonode, ed._merged(edge_data))

    def createReference(self, fromnode, tonode, edge_data="direct"):
        """
        Create a reference from fromnode to tonode
        """
        return super(ModuleGraph, self).createReference(
            fromnode, tonode, edge_data=edge_data
        )

    def findNode(self, name, create_nspkg=True):
        """
        Graph node uniquely identified by the passed fully-qualified module
        name if this module has been added to the graph _or_ `None` otherwise.
        
        If (in order):
        
        . A namespace package with this identifier exists _and_ the passed
          `create_nspkg` parameter is `True`, this package will be
          instantiated and returned.
        . A lazy node with this identifier and:
          * No dependencies exists, this node will be instantiated and
            returned.
          * Dependencies exists, this node and all transitive dependencies of
            this node be instantiated and this node returned.
        . A non-lazy node with this identifier exists, this node will be
          returned as is.
        
        Parameters
        ----------
        name : str
            Fully-qualified name of the module whose graph node is to be found.
        create_nspkg : bool
            Whether or not to implicitly instantiate namespace packages. If
            `True` _and_ this name is that of a previously registered namespace
            package (i.e., in `self.nspackages`) not already added to the
            graph, this package will be added to the graph. Defaults to `True`.
        
        Returns
        ----------
        Node
            Graph node of this module if added to the graph _or_ `None`
            otherwise.
        """
        data = super(ModuleGraph, self).findNode(name)
        if data is not None:
            return data
        if name in self.lazynodes:
            deps = self.lazynodes.pop(name)
            if deps is None:
                m = self.createNode(ExcludedModule, name)
            else:
                if isinstance(deps, Alias):
                    other = self._safe_import_hook(deps, None, None).pop()
                    m = self.createNode(AliasNode, name, other)
                    self.implyNodeReference(m, other)
                else:
                    m = self._safe_import_hook(name, None, None).pop()
                    for dep in deps:
                        self.implyNodeReference(m, dep)

            return m
        if name in self.nspackages and create_nspkg:
            pathnames = self.nspackages.pop(name)
            m = self.createNode(NamespacePackage, name)
            m.filename = "-"
            m.packagepath = _namespace_package_path(name, pathnames, self.path)
            m.packagepath = m.packagepath + self._package_path_map.get(name, [])
            return m

    def run_script(self, pathname, caller=None):
        """
        Create a node by path (not module name).  It is expected to be a Python
        source file, and will be scanned for dependencies.
        """
        self.msg(2, "run_script", pathname)
        pathname = os.path.realpath(pathname)
        m = self.findNode(pathname)
        if m is not None:
            return m
        if sys.version_info[0] != 2:
            with open(pathname, "rb") as (fp):
                encoding = util.guess_encoding(fp)
            with open(pathname, _READ_MODE, encoding=encoding) as (fp):
                contents = fp.read() + "\n"
            if contents.startswith(BOM):
                contents = contents[1:]
            else:
                with open(pathname, _READ_MODE) as (fp):
                    contents = fp.read() + "\n"
            co_ast = compile(contents, pathname, "exec", ast.PyCF_ONLY_AST, True)
            co = compile(co_ast, pathname, "exec", 0, True)
            m = self.createNode(Script, pathname)
            self._updateReference(caller, m, None)
            self._scan_code(m, co, co_ast)
            m.code = co
            if self.replace_paths:
                m.code = self._replace_paths_in_code(m.code)
            return m

    def import_hook(
        self,
        target_module_partname,
        source_module=None,
        target_attr_names=None,
        level=DEFAULT_IMPORT_LEVEL,
        edge_attr=None,
    ):
        """
        Import the module with the passed name, all parent packages of this
        module, _and_ all submodules and attributes in this module with the
        passed names from the previously imported caller module signified by
        the passed graph node.
        
        Unlike most import methods (e.g., `_safe_import_hook()`), this method
        is designed to be publicly called by both external and internal
        callers and hence is public.
        
        Parameters
        ----------
        target_module_partname : str
            Partially-qualified name of the target module to be imported. See
            `_safe_import_hook()` for further details.
        source_module : Node
            Graph node for the previously imported **source module** (i.e.,
            module containing the `import` statement triggering the call to
            this method) _or_ `None` if this module is to be imported in a
            "disconnected" manner. **Passing `None` is _not_ recommended.**
            Doing so produces a disconnected graph in which the graph node
            created for the module to be imported will be disconnected and
            hence unreachable from all other nodes -- which frequently causes
            subtle issues in external callers (namely PyInstaller, which
            silently ignores unreachable nodes).
        target_attr_names : list
            List of the unqualified names of all submodules and attributes to
            be imported from the module to be imported if this is a "from"-
            style import (e.g., `[encode_base64, encode_noop]` for the import
            `from email.encoders import encode_base64, encode_noop`) _or_
            `None` otherwise.
        level : int
            Whether to perform an absolute or relative import. See
            `_safe_import_hook()` for further details.
        
        Returns
        ----------
        list
            List of the graph nodes created for all modules explicitly imported
            by this call, including the passed module and all submodules listed
            in `target_attr_names` _but_ excluding all parent packages
            implicitly imported by this call. If `target_attr_names` is `None`
            or the empty list, this is guaranteed to be a list of one element:
            the graph node created for the passed module.
        
        Raises
        ----------
        ImportError
            If the target module to be imported is unimportable.
        """
        self.msg(
            3,
            "_import_hook",
            target_module_partname,
            source_module,
            source_module,
            level,
        )
        source_package = self._determine_parent(source_module)
        target_package, target_module_partname = self._find_head_package(
            source_package, target_module_partname, level
        )
        target_module = self._load_tail(target_package, target_module_partname)
        target_modules = [target_module]
        if target_attr_names:
            if isinstance(target_module, Package):
                for target_submodule in self._import_importable_package_submodules(
                    target_module, target_attr_names
                ):
                    if target_submodule not in target_modules:
                        target_modules.append(target_submodule)

        for target_module in target_modules:
            self._updateReference(source_module, target_module, edge_data=edge_attr)

        return target_modules

    def _determine_parent(self, caller):
        """
        Determine the package containing a node.
        """
        self.msgin(4, "determine_parent", caller)
        parent = None
        if caller:
            pname = caller.identifier
            if isinstance(caller, Package):
                parent = caller
            else:
                if "." in pname:
                    pname = pname[: pname.rfind(".")]
                    parent = self.findNode(pname)
                else:
                    if caller.packagepath:
                        parent = self.findNode(pname)
                self.msgout(4, "determine_parent ->", parent)
                return parent

    def _find_head_package(
        self, source_package, target_module_partname, level=DEFAULT_IMPORT_LEVEL
    ):
        """
        Import the target package providing the target module with the passed
        name to be subsequently imported from the previously imported source
        package corresponding to the passed graph node.
        
        Parameters
        ----------
        source_package : Package
            Graph node for the previously imported **source package** (i.e.,
            package containing the module containing the `import` statement
            triggering the call to this method) _or_ `None` if this module is
            to be imported in a "disconnected" manner. **Passing `None` is
            _not_ recommended.** See the `_import_hook()` method for further
            details.
        target_module_partname : str
            Partially-qualified name of the target module to be imported. See
            `_safe_import_hook()` for further details.
        level : int
            Whether to perform absolute or relative imports. See the
            `_safe_import_hook()` method for further details.
        
        Returns
        ----------
        (target_package, target_module_tailname)
            2-tuple describing the imported target package, where:
            * `target_package` is the graph node created for this package.
            * `target_module_tailname` is the unqualified name of the target
              module to be subsequently imported (e.g., `text` when passed a
              `target_module_partname` of `email.mime.text`).
        
        Raises
        ----------
        ImportError
            If the package to be imported is unimportable.
        """
        self.msgin(
            4, "find_head_package", source_package, target_module_partname, level
        )
        if "." in target_module_partname:
            target_module_headname, target_module_tailname = target_module_partname.split(
                ".", 1
            )
        else:
            target_module_headname = target_module_partname
            target_module_tailname = ""
        if level == ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL:
            if source_package:
                target_package_name = (
                    source_package.identifier + "." + target_module_headname
                )
            else:
                target_package_name = target_module_headname
        else:
            if level == ABSOLUTE_IMPORT_LEVEL:
                target_package_name = target_module_headname
                source_package = None
            else:
                if source_package is None:
                    self.msg(2, "Relative import outside of package")
                    raise ImportError(
                        "Relative import outside of package (name=%r, parent=%r, level=%r)"
                        % (target_module_partname, source_package, level)
                    )
                for i in range(level - 1):
                    if "." not in source_package.identifier:
                        self.msg(2, "Relative import outside of package")
                        raise ImportError(
                            "Relative import outside of package (name=%r, parent=%r, level=%r)"
                            % (target_module_partname, source_package, level)
                        )
                    p_fqdn = source_package.identifier.rsplit(".", 1)[0]
                    new_parent = self.findNode(p_fqdn)
                    if new_parent is None:
                        self.msg(2, "Relative import outside of package")
                        raise ImportError(
                            "Relative import outside of package (name=%r, parent=%r, level=%r)"
                            % (target_module_partname, source_package, level)
                        )
                    if not new_parent is not source_package:
                        raise AssertionError((new_parent, source_package))
                    source_package = new_parent

                if target_module_headname:
                    target_package_name = (
                        source_package.identifier + "." + target_module_headname
                    )
                else:
                    target_package_name = source_package.identifier
                target_package = self._safe_import_module(
                    target_module_headname, target_package_name, source_package
                )
                if target_package is None:
                    if source_package is not None:
                        target_package_name = target_module_headname
                        source_package = None
                        target_package = self._safe_import_module(
                            target_module_headname, target_package_name, source_package
                        )
                if target_package is not None:
                    self.msgout(
                        4,
                        "find_head_package ->",
                        (target_package, target_module_tailname),
                    )
                    return (target_package, target_module_tailname)
                self.msgout(
                    4, "raise ImportError: No module named", target_package_name
                )
                raise ImportError("No module named " + target_package_name)

    def _load_tail(self, package, submodule_name):
        """
        Import the submodule with the passed name and all parent packages of
        this module from the previously imported parent package corresponding
        to the passed graph node.
        
        Parameters
        ----------
        package : Package
            Graph node of the previously imported package containing this
            submodule.
        submodule_name : str
            Name of the submodule to be imported in either qualified (e.g.,
            `email.mime.base`) or unqualified (e.g., `base`) form.
        
        Returns
        ----------
        Node
            Graph node created for this submodule.
        
        Raises
        ----------
        ImportError
            If this submodule is unimportable.
        """
        self.msgin(4, "load_tail", package, submodule_name)
        submodule = package
        while submodule_name:
            i = submodule_name.find(".")
            if i < 0:
                i = len(submodule_name)
            head, submodule_name = submodule_name[:i], submodule_name[i + 1 :]
            mname = "%s.%s" % (submodule.identifier, head)
            submodule = self._safe_import_module(head, mname, submodule)
            if submodule is None:
                self.msgout(4, "raise ImportError: No module named", mname)
                raise ImportError("No module named " + repr(mname))

        self.msgout(4, "load_tail ->", submodule)
        return submodule

    def _import_importable_package_submodules(self, package, attr_names):
        """
        Generator importing and yielding each importable submodule (of the
        previously imported package corresponding to the passed graph node)
        whose unqualified name is in the passed list.
        
        Elements of this list that are _not_ importable submodules of this
        package are either:
        
        * Ignorable attributes (e.g., classes, globals) defined at the top
          level of this package's `__init__` submodule, which will be ignored.
        * Else, unignorable unimportable submodules, in which case an
          exception is raised.
        
        Parameters
        ----------
        package : Package
            Graph node of the previously imported package containing the
            modules to be imported and yielded.
        
        attr_names : list
            List of the unqualified names of all attributes of this package to
            attempt to import as submodules. This list will be internally
            converted into a set, safely ignoring any duplicates in this list
            (e.g., reducing the "from"-style import
            `from foo import bar, car, far, bar, car, far` to merely
            `from foo import bar, car, far`).
        
        Yields
        ----------
        Node
            Graph node created for the currently imported submodule.
        
        Raises
        ----------
        ImportError
            If any attribute whose name is in `attr_names` is neither:
            * An importable submodule of this package.
            * An ignorable global attribute (e.g., class, variable) defined at
              the top level of this package's `__init__` submodule.
            In this case, this attribute _must_ be an unimportable submodule of
            this package.
        """
        attr_names = set(attr_names)
        self.msgin(4, "_import_importable_package_submodules", package, attr_names)
        if "*" in attr_names:
            attr_names.update(self._find_all_submodules(package))
            attr_names.remove("*")
        for attr_name in attr_names:
            submodule = package.get_submodule_or_none(attr_name)
            submodule_name = submodule is None and package.identifier + "." + attr_name
            submodule = self._safe_import_module(attr_name, submodule_name, package)
            if submodule is None:
                if package.is_global_attr(attr_name):
                    self.msg(
                        4,
                        "_import_importable_package_submodules: ignoring from-imported global",
                        package.identifier,
                        attr_name,
                    )
                    continue
                else:
                    raise ImportError("No module named " + submodule_name)
                yield submodule

        self.msgin(4, "_import_importable_package_submodules ->")

    def _find_all_submodules(self, m):
        if not m.packagepath:
            return
        for path in m.packagepath:
            try:
                names = zipio.listdir(path)
            except (os.error, IOError):
                self.msg(2, "can't list directory", path)
                continue

            for info in (moduleInfoForPath(p) for p in names):
                if info is None:
                    continue
                if info[0] != "__init__":
                    yield info[0]

    def alias_module(self, src_module_name, trg_module_name):
        """
        Alias the source module to the target module with the passed names.
        
        This method ensures that the next call to findNode() given the target
        module name will resolve this alias. This includes importing and adding
        a graph node for the source module if needed as well as adding a
        reference from the target to source module.
        
        Parameters
        ----------
        src_module_name : str
            Fully-qualified name of the existing **source module** (i.e., the
            module being aliased).
        trg_module_name : str
            Fully-qualified name of the non-existent **target module** (i.e.,
            the alias to be created).
        """
        self.msg(3, 'alias_module "%s" -> "%s"' % (src_module_name, trg_module_name))
        if not isinstance(src_module_name, str):
            raise AssertionError('"%s" not a module name.' % str(src_module_name))
        if not isinstance(trg_module_name, str):
            raise AssertionError('"%s" not a module name.' % str(trg_module_name))
        trg_module = self.findNode(trg_module_name)
        if trg_module is not None:
            if not (
                isinstance(trg_module, AliasNode)
                and trg_module.identifier == src_module_name
            ):
                raise ValueError(
                    'Target module "%s" already imported as "%s".'
                    % (trg_module_name, trg_module)
                )
        self.lazynodes[trg_module_name] = Alias(src_module_name)

    def add_module(self, module):
        """
        Add the passed module node to the graph if not already added.
        
        If that module has a parent module or package with a previously added
        node, this method also adds a reference from this module node to its
        parent node and adds this module node to its parent node's namespace.
        
        This high-level method wraps the low-level `addNode()` method, but is
        typically _only_ called by graph hooks adding runtime module nodes. For
        all other node types, the `import_module()` method should be called.
        
        Parameters
        ----------
        module : BaseModule
            Graph node of the module to be added.
        """
        self.msg(3, "add_module", module)
        module_added = self.findNode(module.identifier)
        if module_added is None:
            self.addNode(module)
        else:
            if not module == module_added:
                raise AssertionError(
                    'Newly added module "%s" != previously added module "%s".'
                    % (str(module), str(module_added))
                )
        parent_name, _, module_basename = module.identifier.rpartition(".")
        if parent_name:
            parent = self.findNode(parent_name)
            if parent is None:
                self.msg(4, "add_module parent not found:", parent_name)
            else:
                self.createReference(module, parent)
                parent.add_submodule(module_basename, module)

    def append_package_path(self, package_name, directory):
        """
        Modulegraph does a good job at simulating Python's, but it can not
        handle packagepath '__path__' modifications packages make at runtime.
        
        Therefore there is a mechanism whereby you can register extra paths
        in this map for a package, and it will be honored.
        
        NOTE: This method has to be called before a package is resolved by
              modulegraph.
        
        Parameters
        ----------
        module : str
            Fully-qualified module name.
        directory : str
            Absolute or relative path of the directory to append to the
            '__path__' attribute.
        """
        paths = self._package_path_map.setdefault(package_name, [])
        paths.append(directory)

    def _safe_import_module(self, module_partname, module_name, parent_module):
        """
        Create a new graph node for the module with the passed name under the
        parent package signified by the passed graph node _without_ raising
        `ImportError` exceptions.
        
        If this module has already been imported, this module's existing graph
        node will be returned; else if this module is importable, a new graph
        node will be added for this module and returned; else this module is
        unimportable, in which case `None` will be returned. Like the
        `_safe_import_hook()` method, this method does _not_ raise
        `ImportError` exceptions when this module is unimportable.
        
        Parameters
        ----------
        module_partname : str
            Unqualified name of the module to be imported (e.g., `text`).
        module_name : str
            Fully-qualified name of this module (e.g., `email.mime.text`).
        parent_module : Package
            Graph node of the previously imported parent module containing this
            submodule _or_ `None` if this is a top-level module (i.e.,
            `module_name` contains no `.` delimiters). This parent module is
            typically but _not_ always a package (e.g., the `os.path` submodule
            contained by the `os` module).
        
        Returns
        ----------
        Node
            Graph node created for this module _or_ `None` if this module is
            unimportable.
        """
        self.msgin(3, "safe_import_module", module_partname, module_name, parent_module)
        module = self.findNode(module_name)
        if module is None:
            search_dirs = None
            file_handle = None
            if parent_module is not None:
                if parent_module.packagepath is not None:
                    search_dirs = parent_module.packagepath
                else:
                    self.msgout(
                        3,
                        "safe_import_module -> None (parent_parent.packagepath is None)",
                    )
                    return
                try:
                    try:
                        file_handle, pathname, metadata = self._find_module(
                            module_partname, search_dirs, parent_module
                        )
                    except ImportError as exc:
                        self.msgout(3, "safe_import_module -> None (%r)" % exc)
                        return

                    module = self._load_module(
                        module_name, file_handle, pathname, metadata
                    )
                finally:
                    if file_handle is not None:
                        file_handle.close()

            if parent_module is not None:
                self.msg(
                    4,
                    "safe_import_module create reference",
                    module,
                    "->",
                    parent_module,
                )
                self._updateReference(
                    module,
                    parent_module,
                    edge_data=DependencyInfo(
                        conditional=False,
                        fromlist=False,
                        function=False,
                        tryexcept=False,
                    ),
                )
                parent_module.add_submodule(module_partname, module)
            self.msgout(3, "safe_import_module ->", module)
            return module

    def _load_module(self, fqname, fp, pathname, info):
        suffix, mode, typ = info
        self.msgin(2, "load_module", fqname, fp and "fp", pathname)
        if typ == imp.PKG_DIRECTORY:
            if isinstance(mode, (list, tuple)):
                packagepath = mode
            else:
                packagepath = []
            m = self._load_package(fqname, pathname, packagepath)
            self.msgout(2, "load_module ->", m)
            return m
        if typ == imp.PY_SOURCE:
            contents = fp.read()
            if isinstance(contents, bytes):
                contents += b"\n"
            else:
                contents += "\n"
            try:
                co = compile(contents, pathname, "exec", ast.PyCF_ONLY_AST, True)
            except SyntaxError:
                co = None
                cls = InvalidSourceModule
                self.msg(2, "load_module: InvalidSourceModule", pathname)
            else:
                cls = SourceModule

        else:
            if typ == imp.PY_COMPILED:
                if fp.read(4) != imp.get_magic():
                    self.msg(2, "load_module: InvalidCompiledModule", pathname)
                    co = None
                    cls = InvalidCompiledModule
                else:
                    fp.read(4)
                try:
                    co = marshal.loads(fp.read())
                    cls = CompiledModule
                except Exception:
                    co = None
                    cls = InvalidCompiledModule

            else:
                if typ == imp.C_BUILTIN:
                    cls = BuiltinModule
                    co = None
                else:
                    cls = Extension
                    co = None
                m = self.createNode(cls, fqname)
                m.filename = pathname
        if co is not None:
            try:
                if isinstance(co, ast.AST):
                    co_ast = co
                    co = compile(co_ast, pathname, "exec", 0, True)
                else:
                    co_ast = None
                self._scan_code(m, co, co_ast)
                if self.replace_paths:
                    co = self._replace_paths_in_code(co)
                m.code = co
            except SyntaxError:
                self.msg(1, "load_module: SyntaxError in ", pathname)
                cls = InvalidSourceModule
                m = self.createNode(cls, fqname)

            self.msgout(2, "load_module ->", m)
            return m

    def _safe_import_hook(
        self,
        target_module_partname,
        source_module,
        target_attr_names,
        level=DEFAULT_IMPORT_LEVEL,
        edge_attr=None,
    ):
        """
        Import the module with the passed name and all parent packages of this
        module from the previously imported caller module signified by the
        passed graph node _without_ raising `ImportError` exceptions.
        
        This method wraps the lowel-level `_import_hook()` method. On catching
        an `ImportError` exception raised by that method, this method creates
        and adds a `MissingNode` instance describing the unimportable module to
        the graph instead.
        
        Parameters
        ----------
        target_module_partname : str
            Partially-qualified name of the module to be imported. If `level`
            is:
            * `ABSOLUTE_OR_RELATIVE_IMPORT_LEVEL` (e.g., the Python 2 default)
              or a positive integer (e.g., an explicit relative import), the
              fully-qualified name of this module is the concatenation of the
              fully-qualified name of the caller module's package and this
              parameter.
            * `ABSOLUTE_IMPORT_LEVEL` (e.g., the Python 3 default), this name
              is already fully-qualified.
            * A non-negative integer (e.g., `1`), this name is typically the
              empty string. In this case, this is a "from"-style relative
              import (e.g., "from . import bar") and the fully-qualified name
              of this module is dynamically resolved by import machinery.
        source_module : Node
            Graph node for the previously imported **caller module** (i.e.,
            module containing the `import` statement triggering the call to
            this method) _or_ `None` if this module is to be imported in a
            "disconnected" manner. **Passing `None` is _not_ recommended.**
            Doing so produces a disconnected graph in which the graph node
            created for the module to be imported will be disconnected and
            hence unreachable from all other nodes -- which frequently causes
            subtle issues in external callers (e.g., PyInstaller, which
            silently ignores unreachable nodes).
        target_attr_names : list
            List of the unqualified names of all submodules and attributes to
            be imported via a `from`-style import statement from this target
            module if any (e.g., the list `[encode_base64, encode_noop]` for
            the import `from email.encoders import encode_base64, encode_noop`)
            _or_ `None` otherwise. Ignored unless `source_module` is the graph
            node of a package (i.e., is an instance of the `Package` class).
            Why? Because:
            * Consistency. The `_import_importable_package_submodules()`
              method accepts a similar list applicable only to packages.
            * Efficiency. Unlike packages, modules cannot physically contain
              submodules. Hence, any target module imported via a `from`-style
              import statement as an attribute from another target parent
              module must itself have been imported in that target parent
              module. The import statement responsible for that import must
              already have been previously parsed by `ModuleGraph`, in which
              case that target module will already be frozen by PyInstaller.
              These imports are safely ignorable here.
        level : int
            Whether to perform an absolute or relative import. This parameter
            corresponds exactly to the parameter of the same name accepted by
            the `__import__()` built-in: "The default is -1 which indicates
            both absolute and relative imports will be attempted. 0 means only
            perform absolute imports. Positive values for level indicate the
            number of parent directories to search relative to the directory of
            the module calling `__import__()`." Defaults to -1 under Python 2
            and 0 under Python 3. Since this default depends on the major
            version of the current Python interpreter, depending on this
            default can result in unpredictable and non-portable behaviour.
            Callers are strongly recommended to explicitly pass this parameter
            rather than implicitly accept this default.
        
        Returns
        ----------
        list
            List of the graph nodes created for all modules explicitly imported
            by this call, including the passed module and all submodules listed
            in `target_attr_names` _but_ excluding all parent packages
            implicitly imported by this call. If `target_attr_names` is either
            `None` or the empty list, this is guaranteed to be a list of one
            element: the graph node created for the passed module. As above,
            `MissingNode` instances are created for all unimportable modules.
        """
        self.msg(
            3,
            "_safe_import_hook",
            target_module_partname,
            source_module,
            target_attr_names,
            level,
        )

        def is_swig_candidate():
            return (
                source_module is not None
                and target_attr_names is None
                and level == ABSOLUTE_IMPORT_LEVEL
                and type(source_module) is SourceModule
                and target_module_partname
                == "_" + source_module.identifier.rpartition(".")[2]
                and sys.version_info[0] == 3
            )

        def is_swig_wrapper(source_module):
            with open(source_module.filename, "rb") as (source_module_file):
                encoding = util.guess_encoding(source_module_file)
            with open(source_module.filename, _READ_MODE, encoding=encoding) as (
                source_module_file
            ):
                first_line = source_module_file.readline()
            self.msg(5, "SWIG wrapper candidate first line: %r" % first_line)
            return "automatically generated by SWIG" in first_line

        target_modules = None
        is_swig_import = None
        try:
            target_modules = self.import_hook(
                target_module_partname,
                source_module,
                target_attr_names=None,
                level=level,
                edge_attr=edge_attr,
            )
        except ImportError as msg:
            if is_swig_candidate():
                self.msg(
                    4,
                    "SWIG import candidate (name=%r, caller=%r, level=%r)"
                    % (target_module_partname, source_module, level),
                )
                is_swig_import = is_swig_wrapper(source_module)
                if is_swig_import:
                    target_attr_names = [target_module_partname]
                    target_module_partname = ""
                    level = 1
                    self.msg(
                        2,
                        "SWIG import (caller=%r, fromlist=%r, level=%r)"
                        % (source_module, target_attr_names, level),
                    )
                    try:
                        target_modules = self.import_hook(
                            target_module_partname,
                            source_module,
                            target_attr_names=None,
                            level=level,
                            edge_attr=edge_attr,
                        )
                    except ImportError as msg:
                        self.msg(2, "SWIG ImportError:", str(msg))

                    if target_modules is None:
                        self.msg(2, "ImportError:", str(msg))
                        target_module = self.createNode(
                            MissingModule,
                            _path_from_importerror(msg, target_module_partname),
                        )
                        self._updateReference(
                            source_module, target_module, edge_data=edge_attr
                        )
                        target_modules = [target_module]

        if not len(target_modules) == 1:
            raise AssertionError(
                (
                    "Expected import_hook() toreturn only one module but received: {}"
                ).format(target_modules)
            )
        target_module = target_modules[0]
        if isinstance(target_module, MissingModule):
            if is_swig_import is None:
                if is_swig_candidate():
                    if is_swig_wrapper(source_module):
                        self.removeNode(target_module)
                        target_modules = self._safe_import_hook(
                            target_module_partname,
                            source_module,
                            target_attr_names=None,
                            level=1,
                            edge_attr=edge_attr,
                        )
                        return target_modules
                    if isinstance(edge_attr, DependencyInfo):
                        edge_attr = edge_attr._replace(fromlist=True)
                    if target_attr_names:
                        if isinstance(target_module, Package):
                            for target_submodule_partname in target_attr_names:
                                if target_module.is_submodule(
                                    target_submodule_partname
                                ):
                                    target_submodule = target_module.get_submodule(
                                        target_submodule_partname
                                    )
                                    if target_submodule is not None:
                                        pass
                                    if target_submodule not in target_modules:
                                        self._updateReference(
                                            source_module,
                                            target_submodule,
                                            edge_data=edge_attr,
                                        )
                                        target_modules.append(target_submodule)
                                        continue
                                    target_submodule_name = (
                                        target_module.identifier
                                        + "."
                                        + target_submodule_partname
                                    )
                                    target_submodule = self.findNode(
                                        target_submodule_name
                                    )
                                    if target_submodule is None:
                                        try:
                                            self.import_hook(
                                                target_module_partname,
                                                source_module,
                                                target_attr_names=[
                                                    target_submodule_partname
                                                ],
                                                level=level,
                                                edge_attr=edge_attr,
                                            )
                                            target_submodule = self.findNode(
                                                target_submodule_name
                                            )
                                            if target_submodule is None:
                                                if not target_module.is_global_attr(
                                                    target_submodule_partname
                                                ):
                                                    raise AssertionError(
                                                        (
                                                            "No global named {} in {}.__init__"
                                                        ).format(
                                                            target_submodule_partname,
                                                            target_module.identifier,
                                                        )
                                                    )
                                                self.msg(
                                                    4,
                                                    "_safe_import_hook",
                                                    "ignoring imported non-module global",
                                                    target_module.identifier,
                                                    target_submodule_partname,
                                                )
                                                continue
                                            if is_swig_import:
                                                if self.findNode(
                                                    target_submodule_partname
                                                ):
                                                    self.msg(
                                                        2,
                                                        "SWIG import error: %r basename %r already exists"
                                                        % (
                                                            target_submodule_name,
                                                            target_submodule_partname,
                                                        ),
                                                    )
                                                else:
                                                    self.msg(
                                                        4,
                                                        "SWIG import renamed from %r to %r"
                                                        % (
                                                            target_submodule_name,
                                                            target_submodule_partname,
                                                        ),
                                                    )
                                                    target_submodule.identifier = (
                                                        target_submodule_partname
                                                    )
                                        except ImportError as msg:
                                            self.msg(2, "ImportError:", str(msg))
                                            target_submodule = self.createNode(
                                                MissingModule, target_submodule_name
                                            )

                                        target_module.add_submodule(
                                            target_submodule_partname, target_submodule
                                        )
                                        if target_submodule is not None:
                                            self._updateReference(
                                                target_module,
                                                target_submodule,
                                                edge_data=edge_attr,
                                            )
                                            self._updateReference(
                                                source_module,
                                                target_submodule,
                                                edge_data=edge_attr,
                                            )
                                            if target_submodule not in target_modules:
                                                target_modules.append(target_submodule)

                    return target_modules

    def _scan_code(self, module, module_code_object, module_code_object_ast=None):
        """
        Parse and add all import statements from the passed code object of the
        passed source module to this graph, recursively.
        
        **This method is at the root of all `ModuleGraph` recursion.**
        Recursion begins here and ends when all import statements in all code
        objects of all modules transitively imported by the source module
        passed to the first call to this method have been added to the graph.
        Specifically, this method:
        
        1. If the passed `module_code_object_ast` parameter is non-`None`,
           parses all import statements from this object.
        2. Else, parses all import statements from the passed
           `module_code_object` parameter.
        1. For each such import statement:
           1. Adds to this `ModuleGraph` instance:
              1. Nodes for all target modules of these imports.
              1. Directed edges from this source module to these target
                 modules.
           2. Recursively calls this method with these target modules.
        
        Parameters
        ----------
        module : Node
            Graph node of the module to be parsed.
        module_code_object : PyCodeObject
            Code object providing this module's disassembled Python bytecode.
            Ignored unless `module_code_object_ast` is `None`.
        module_code_object_ast : optional[ast.AST]
            Optional abstract syntax tree (AST) of this module if any or `None`
            otherwise. Defaults to `None`, in which case the passed
            `module_code_object` is parsed instead.
        """
        module._deferred_imports = []
        if module_code_object_ast is not None:
            self._scan_ast(module, module_code_object_ast)
            self._scan_bytecode(module, module_code_object, is_scanning_imports=False)
        else:
            self._scan_bytecode(module, module_code_object, is_scanning_imports=True)
        self._process_imports(module)

    def _scan_ast(self, module, module_code_object_ast):
        """
        Parse and add all import statements from the passed abstract syntax
        tree (AST) of the passed source module to this graph, non-recursively.
        
        Parameters
        ----------
        module : Node
            Graph node of the module to be parsed.
        module_code_object_ast : ast.AST
            Abstract syntax tree (AST) of this module to be parsed.
        """
        visitor = _Visitor(self, module)
        visitor.visit(module_code_object_ast)

    def _scan_bytecode(self, module, module_code_object, is_scanning_imports):
        """
        Parse and add all import statements from the passed code object of the
        passed source module to this graph, non-recursively.
        
        This method parses all reasonably parsable operations (i.e., operations
        that are both syntactically and semantically parsable _without_
        requiring Turing-complete interpretation) directly or indirectly
        involving module importation from this code object. This includes:
        
        * `IMPORT_NAME`, denoting an import statement. Ignored unless
          the passed `is_scanning_imports` parameter is `True`.
        * `STORE_NAME` and `STORE_GLOBAL`, denoting the
          declaration of a global attribute (e.g., class, variable) in this
          module. This method stores each such declaration for subsequent
          lookup. While global attributes are usually irrelevant to import
          parsing, they remain the only means of distinguishing erroneous
          non-ignorable attempts to import non-existent submodules of a package
          from successful ignorable attempts to import existing global
          attributes of a package's `__init__` submodule (e.g., the `bar` in
          `from foo import bar`, which is either a non-ignorable submodule of
          `foo` or an ignorable global attribute of `foo.__init__`).
        * `DELETE_NAME` and `DELETE_GLOBAL`, denoting the
          undeclaration of a previously declared global attribute in this
          module.
        
        Since `ModuleGraph` is _not_ intended to replicate the behaviour of a
        full-featured Turing-complete Python interpreter, this method ignores
        operations that are _not_ reasonably parsable from this code object --
        even those directly or indirectly involving module importation. This
        includes:
        
        * `STORE_ATTR(namei)`, implementing `TOS.name = TOS1`. If `TOS` is the
          name of a target module currently imported into the namespace of the
          passed source module, this opcode would ideally be parsed to add that
          global attribute to that target module. Since this addition only
          conditionally occurs on the importation of this source module and
          execution of the code branch in this module performing this addition,
          however, that global _cannot_ be unconditionally added to that target
          module. In short, only Turing-complete behaviour suffices.
        * `DELETE_ATTR(namei)`, implementing `del TOS.name`. If `TOS` is the
          name of a target module currently imported into the namespace of the
          passed source module, this opcode would ideally be parsed to remove
          that global attribute from that target module. Again, however, only
          Turing-complete behaviour suffices.
        
        Parameters
        ----------
        module : Node
            Graph node of the module to be parsed.
        module_code_object : PyCodeObject
            Code object of the module to be parsed.
        is_scanning_imports : bool
            `True` only if this method is parsing import statements from
            `IMPORT_NAME` opcodes. If `False`, no import statements will be
            parsed. This parameter is typically:
            * `True` when parsing this module's code object for such imports.
            * `False` when parsing this module's abstract syntax tree (AST)
              (rather than code object) for such imports. In this case, that
              parsing will have already parsed import statements, which this
              parsing must avoid repeating.
        """
        level = None
        fromlist = None
        prev_insts = deque(maxlen=2)
        for inst in util.iterate_instructions(module_code_object):
            if not inst:
                continue
            if inst.opname == "IMPORT_NAME":
                if not is_scanning_imports:
                    pass
                else:
                    if not prev_insts[-2].opname == "LOAD_CONST":
                        raise AssertionError
                    if not prev_insts[-1].opname == "LOAD_CONST":
                        raise AssertionError
                    level = prev_insts[-2].argval
                    fromlist = prev_insts[-1].argval
                    if not fromlist is None:
                        if not type(fromlist) is tuple:
                            raise AssertionError
                    target_module_partname = inst.argval
                    have_star = False
                    if fromlist is not None:
                        fromlist = uniq(fromlist)
                        if "*" in fromlist:
                            fromlist.remove("*")
                            have_star = True
                    module._deferred_imports.append(
                        (
                            have_star,
                            (target_module_partname, module, fromlist, level),
                            {},
                        )
                    )
            else:
                if inst.opname in ("STORE_NAME", "STORE_GLOBAL"):
                    name = inst.argval
                    module.add_global_attr(name)
                else:
                    if inst.opname in ("DELETE_NAME", "DELETE_GLOBAL"):
                        name = inst.argval
                        module.remove_global_attr_if_found(name)
                prev_insts.append(inst)

    def _process_imports(self, source_module):
        """
        Graph all target modules whose importations were previously parsed from
        the passed source module by a prior call to the `_scan_code()` method
        and methods call by that method (e.g., `_scan_ast()`,
        `_scan_bytecode()`, `_scan_bytecode_stores()`).
        
        Parameters
        ----------
        source_module : Node
            Graph node of the source module to graph target imports for.
        """
        if not source_module._deferred_imports:
            return
        for have_star, import_info, kwargs in source_module._deferred_imports:
            target_module = (self._safe_import_hook(*import_info, **kwargs))[0]
            if have_star:
                source_module.add_global_attrs_from_module(target_module)
                source_module._starimported_ignored_module_names.update(
                    target_module._starimported_ignored_module_names
                )
                if target_module.code is None:
                    target_module_name = import_info[0]
                    source_module._starimported_ignored_module_names.add(
                        target_module_name
                    )

        source_module._deferred_imports = None

    def _load_package(self, fqname, pathname, pkgpath):
        """
        Called only when an imp.PKG_DIRECTORY is found
        """
        self.msgin(2, "load_package", fqname, pathname, pkgpath)
        newname = _replacePackageMap.get(fqname)
        if newname:
            fqname = newname
        ns_pkgpath = _namespace_package_path(fqname, pkgpath or [], self.path)
        if ns_pkgpath or pkgpath:
            m = self.createNode(NamespacePackage, fqname)
            m.filename = "-"
            m.packagepath = ns_pkgpath
        else:
            m = self.createNode(Package, fqname)
            m.filename = pathname
            if os.path.basename(pathname).startswith("__init__."):
                pathname = os.path.dirname(pathname)
            m.packagepath = [pathname] + ns_pkgpath
        m.packagepath = m.packagepath + self._package_path_map.get(fqname, [])
        try:
            self.msg(2, "find __init__ for %s" % (m.packagepath,))
            fp, buf, stuff = self._find_module("__init__", m.packagepath, parent=m)
        except ImportError:
            pass
        else:
            try:
                self.msg(2, "load __init__ for %s" % (m.packagepath,))
                self._load_module(fqname, fp, buf, stuff)
            finally:
                if fp is not None:
                    fp.close()

        self.msgout(2, "load_package ->", m)
        return m

    def _find_module(self, name, path, parent=None):
        """
        3-tuple describing the physical location of the module with the passed
        name if this module is physically findable _or_ raise `ImportError`.
        
        This high-level method wraps the low-level `modulegraph.find_module()`
        function with additional support for graph-based module caching.
        
        Parameters
        ----------
        name : str
            Fully-qualified name of the Python module to be found.
        path : list
            List of the absolute paths of all directories to search for this
            module _or_ `None` if the default path list `self.path` is to be
            searched.
        parent : Node
            Package containing this module if this module is a submodule of a
            package _or_ `None` if this is a top-level module.
        
        Returns
        ----------
        (file_handle, filename, metadata)
            See `modulegraph._find_module()` for details.
        
        Raises
        ----------
        ImportError
            If this module is _not_ found.
        """
        if parent is not None:
            fullname = parent.identifier + "." + name
        else:
            fullname = name
        node = self.findNode(fullname)
        if node is not None:
            self.msg(3, "find_module: already included?", node)
            raise ImportError(name)
        if path is None:
            if name in sys.builtin_module_names:
                return (None, None, ("", "", imp.C_BUILTIN))
            path = self.path
        return self._find_module_path(fullname, name, path)

    def _find_module_path(self, fullname, module_name, search_dirs):
        """
        3-tuple describing the physical location of the module with the passed
        name if this module is physically findable _or_ raise `ImportError`.
        
        This low-level function is a variant on the standard `imp.find_module()`
        function with additional support for:
        
        * Multiple search paths. The passed list of absolute paths will be
          iteratively searched for the first directory containing a file
          corresponding to this module.
        * Compressed (e.g., zipped) packages.
        
        For efficiency, the higher level `ModuleGraph._find_module()` method
        wraps this function with support for module caching.
        
        Parameters
        ----------
        module_name : str
            Fully-qualified name of the module to be found.
        search_dirs : list
            List of the absolute paths of all directories to search for this
            module (in order). Searching will halt at the first directory
            containing this module.
        
        Returns
        ----------
        (file_handle, filename, metadata)
            3-tuple describing the physical location of this module, where:
            * `file_handle` is an open read-only file handle from which the
              on-disk contents of this module may be read if this is a
              pure-Python module or `None` otherwise (e.g., if this is a
              package or C extension).
            * `filename` is the absolute path of this file.
            * `metadata` is itself a 3-tuple `(filetype, open_mode, imp_type)`.
              See the `_IMPORTABLE_FILETYPE_TO_METADATA` dictionary for
              details.
        
        Raises
        ----------
        ImportError
            If this module is _not_ found.
        """
        self.msgin(4, "_find_module_path <-", fullname, search_dirs)
        path_data = None
        file_handle = None
        namespace_dirs = []
        try:
            for search_dir in search_dirs:
                importer = pkgutil.get_importer(search_dir)
                if importer is None:
                    continue
                if hasattr(importer, "find_loader"):
                    loader, loader_namespace_dirs = importer.find_loader(module_name)
                    namespace_dirs.extend(loader_namespace_dirs)
                else:
                    if hasattr(importer, "find_module"):
                        loader = importer.find_module(module_name)
                    else:
                        raise ImportError(
                            "Module %r importer %r loader unobtainable"
                            % (module_name, importer)
                        )
                    if loader is None:
                        continue
                    metadata = None
                    pathname = None
                    if hasattr(loader, "get_filename"):
                        pathname = loader.get_filename(module_name)
                    else:
                        if hasattr(loader, "path"):
                            pathname = loader.path
                        else:
                            raise ImportError(
                                "Module %r loader %r path unobtainable"
                                % (module_name, loader)
                            )
                if pathname is None:
                    self.msg(4, "_find_module_path path not found", pathname)
                continue
                if hasattr(loader, "is_package"):
                    if loader.is_package(module_name):
                        metadata = ("", "", imp.PKG_DIRECTORY)
                    filetype = os.path.splitext(pathname)[1]
                    if not filetype:
                        raise ImportError(
                            "Non-package module %r path %r has no filetype"
                            % (module_name, pathname)
                        )
                    metadata = _IMPORTABLE_FILETYPE_TO_METADATA.get(filetype, None)
                    if metadata is None:
                        raise ImportError(
                            "Non-package module %r filetype %r unrecognized"
                            % (pathname, filetype)
                        )
                    open_mode = metadata[1]
                    imp_type = metadata[2]
                    if imp_type == imp.C_EXTENSION:
                        pass
                    else:
                        if imp_type == imp.PY_SOURCE:
                            if hasattr(loader, "get_source"):
                                file_handle = StringIO(loader.get_source(module_name))
                            if imp_type == imp.PY_COMPILED:
                                if hasattr(loader, "get_code"):
                                    try:
                                        code_object = loader.get_code(module_name)
                                        if code_object is None:
                                            file_handle = BytesIO(
                                                b"\x00\x00\x00\x00\x00\x00\x00\x00"
                                            )
                                        else:
                                            file_handle = _code_to_file(code_object)
                                    except ImportError:
                                        file_handle = BytesIO(
                                            b"\x00\x00\x00\x00\x00\x00\x00\x00"
                                        )

                                if imp_type == imp.PY_SOURCE:
                                    if sys.version_info[0] == 3:
                                        with open(pathname, "rb") as (file_handle):
                                            encoding = util.guess_encoding(file_handle)
                                        file_handle = open(
                                            pathname, open_mode, encoding=encoding
                                        )
                                    file_handle = open(pathname, open_mode)
                    path_data = (file_handle, pathname, metadata)
                    break
            else:
                if namespace_dirs:
                    path_data = (
                        None,
                        namespace_dirs[0],
                        ("", namespace_dirs, imp.PKG_DIRECTORY),
                    )

        except UnicodeDecodeError as exc:
            self.msgout(1, "_find_module_path -> unicode error", exc)
        except Exception as exc:
            self.msgout(4, "_find_module_path -> exception", exc)
            raise

        self.msgout(4, "_find_module_path ->", path_data)
        if path_data is None:
            raise ImportError("No module named " + repr(module_name))
        return path_data

    def create_xref(self, out=None):
        global contpl
        global contpl_linked
        global entry
        global footer
        global header
        global imports
        if out is None:
            out = sys.stdout
        scripts = []
        mods = []
        for mod in self.flatten():
            name = os.path.basename(mod.identifier)
            if isinstance(mod, Script):
                scripts.append((name, mod))
            else:
                mods.append((name, mod))

        scripts.sort()
        mods.sort()
        scriptnames = [name for name, m in scripts]
        scripts.extend(mods)
        mods = scripts
        title = "modulegraph cross reference for " + (", ").join(scriptnames)
        print(header % {"TITLE": title}, file=out)

        def sorted_namelist(mods):
            lst = [os.path.basename(mod.identifier) for mod in mods if mod]
            lst.sort()
            return lst

        for name, m in mods:
            content = ""
            if isinstance(m, BuiltinModule):
                content = contpl % {"NAME": name, "TYPE": "<i>(builtin module)</i>"}
            else:
                if isinstance(m, Extension):
                    content = contpl % {
                        "NAME": name,
                        "TYPE": "<tt>%s</tt>" % m.filename,
                    }
                else:
                    url = pathname2url(m.filename or "")
                    content = contpl_linked % {
                        "NAME": name,
                        "URL": url,
                        "TYPE": m.__class__.__name__,
                    }
                oute, ince = map(sorted_namelist, self.get_edges(m))
                if oute:
                    links = []
                    for n in oute:
                        links.append('  <a href="#%s">%s</a>\n' % (n, n))

                    links = (" &#8226; ").join(links)
                    content += imports % {"HEAD": "imports", "LINKS": links}
                if ince:
                    links = []
                    for n in ince:
                        links.append('  <a href="#%s">%s</a>\n' % (n, n))

                    links = (" &#8226; ").join(links)
                    content += imports % {"HEAD": "imported by", "LINKS": links}
                print(entry % {"NAME": name, "CONTENT": content}, file=out)

        print(footer, file=out)

    def itergraphreport(self, name="G", flatpackages=()):
        nodes = list(map(self.graph.describe_node, self.graph.iterdfs(self)))
        describe_edge = self.graph.describe_edge
        edges = deque()
        packagenodes = set()
        packageidents = {}
        nodetoident = {}
        inpackages = {}
        mainedges = set()
        flatpackages = dict(flatpackages)

        def nodevisitor(node, data, outgoing, incoming):
            if not isinstance(data, Node):
                return {"label": str(node)}
            else:
                s = "<f0> " + type(data).__name__
                for i, v in enumerate(data.infoTuple()[:1], 1):
                    s += "| <f%d> %s" % (i, v)

                return {"label": s, "shape": "record"}

        def edgevisitor(edge, data, head, tail):
            if data == "orphan":
                return {"style": "dashed"}
            elif data == "pkgref":
                return {"style": "dotted"}
            else:
                return {}

        yield "digraph %s {\n" % (name,)
        attr = dict(rankdir="LR", concentrate="true")
        cpatt = '%s="%s"'
        for item in attr.items():
            yield "\t%s;\n" % (cpatt % item,)

        for node, data, outgoing, incoming in nodes:
            nodetoident[node] = getattr(data, "identifier", None)
            if isinstance(data, Package):
                packageidents[data.identifier] = node
                inpackages[node] = set([node])
                packagenodes.add(node)

        for node, data, outgoing, incoming in nodes:
            for edge in (describe_edge(e) for e in outgoing):
                edges.append(edge)

            yield '\t"%s" [%s];\n' % (
                node,
                (",").join(
                    [
                        cpatt % item
                        for item in nodevisitor(node, data, outgoing, incoming).items()
                    ]
                ),
            )
            inside = inpackages.get(node)
            if inside is None:
                inside = inpackages[node] = set()
            ident = nodetoident[node]
            if ident is None:
                continue
            pkgnode = packageidents.get(ident[: ident.rfind(".")])
            if pkgnode is not None:
                inside.add(pkgnode)

        graph = []
        subgraphs = {}
        for key in packagenodes:
            subgraphs[key] = []

        while edges:
            edge, data, head, tail = edges.popleft()
            if (head, tail) in mainedges:
                continue
            mainedges.add((head, tail))
            tailpkgs = inpackages[tail]
            common = inpackages[head] & tailpkgs
            if not common:
                if tailpkgs:
                    usepkgs = sorted(tailpkgs)
                    if len(usepkgs) != 1 or usepkgs[0] != tail:
                        edges.append((edge, data, head, usepkgs[0]))
                        edges.append((edge, "pkgref", usepkgs[-1], tail))
                        continue
                if common:
                    common = common.pop()
                    if tail == common:
                        edges.append((edge, data, tail, head))
                    else:
                        if head == common:
                            subgraphs[common].append((edge, "pkgref", head, tail))
                        else:
                            edges.append((edge, data, common, head))
                            edges.append((edge, data, common, tail))
                else:
                    graph.append((edge, data, head, tail))

        def do_graph(edges, tabs):
            edgestr = tabs + '"%s" -> "%s" [%s];\n'
            for edge, data, head, tail in edges:
                attribs = edgevisitor(edge, data, head, tail)
                yield edgestr % (
                    head,
                    tail,
                    (",").join([cpatt % item for item in attribs.items()]),
                )

        for g, edges in subgraphs.items():
            yield '\tsubgraph "cluster_%s" {\n' % (g,)
            yield '\t\tlabel="%s";\n' % (nodetoident[g],)
            for s in do_graph(edges, "\t\t"):
                yield s

            yield "\t}\n"

        for s in do_graph(graph, "\t"):
            yield s

        yield "}\n"

    def graphreport(self, fileobj=None, flatpackages=()):
        if fileobj is None:
            fileobj = sys.stdout
        fileobj.writelines(self.itergraphreport(flatpackages=flatpackages))

    def report(self):
        """Print a report to stdout, listing the found modules with their
        paths, as well as modules that are missing, or seem to be missing.
        """
        print()
        print("%-15s %-25s %s" % ("Class", "Name", "File"))
        print("%-15s %-25s %s" % ("-----", "----", "----"))
        sorted = [(os.path.basename(mod.identifier), mod) for mod in self.flatten()]
        sorted.sort()
        for name, m in sorted:
            print("%-15s %-25s %s" % (type(m).__name__, name, m.filename or ""))

    def _replace_paths_in_code(self, co):
        new_filename = original_filename = os.path.normpath(co.co_filename)
        for f, r in self.replace_paths:
            f = os.path.join(f, "")
            r = os.path.join(r, "")
            if original_filename.startswith(f):
                new_filename = r + original_filename[len(f) :]
                break
        else:
            return co

        consts = list(co.co_consts)
        for i in range(len(consts)):
            if isinstance(consts[i], type(co)):
                consts[i] = self._replace_paths_in_code(consts[i])

        code_func = type(co)
        if hasattr(co, "co_kwonlyargcount"):
            return code_func(
                co.co_argcount,
                co.co_kwonlyargcount,
                co.co_nlocals,
                co.co_stacksize,
                co.co_flags,
                co.co_code,
                tuple(consts),
                co.co_names,
                co.co_varnames,
                new_filename,
                co.co_name,
                co.co_firstlineno,
                co.co_lnotab,
                co.co_freevars,
                co.co_cellvars,
            )
        else:
            return code_func(
                co.co_argcount,
                co.co_nlocals,
                co.co_stacksize,
                co.co_flags,
                co.co_code,
                tuple(consts),
                co.co_names,
                co.co_varnames,
                new_filename,
                co.co_name,
                co.co_firstlineno,
                co.co_lnotab,
                co.co_freevars,
                co.co_cellvars,
            )
